package com.zlx.mall.basic.web.model;


import com.zlx.mall.basic.common.constant.Constants;
import com.zlx.mall.basic.common.enums.ResponseEnum;
import com.zlx.mall.basic.common.exception.BaseResponseStatusEnum;
import com.zlx.mall.basic.common.model.BaseObject;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Collections;

/**
 * @author qingke
 * @see org.springframework.http.ResponseEntity
 **/
@Data
@EqualsAndHashCode(callSuper = true)
public class BaseResponse extends BaseObject
        implements java.io.Serializable {

    private Integer code;
    private String message;
    private Integer extraCode;
    private String extraMessage;
    private Long timestamp;
    private Object data;

    private BaseResponse(Object data) {
        this.timestamp = System.currentTimeMillis();
        this.data = data;
    }

    public BaseResponse status(Integer code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }

    public BaseResponse status(ResponseEnum status) {
        return status(status.getCode(), status.getMessage());
    }

    public BaseResponse extra(Integer code, String message) {
        this.extraCode = code;
        this.extraMessage = message;
        return this;
    }

    public BaseResponse extra(ResponseEnum status) {
        return extra(status.getCode(), status.getMessage());
    }

    public BaseResponse code(Integer code) {
        this.code = code;
        return this;
    }

    public BaseResponse message(String message) {
        this.message = message;
        return this;
    }

    public BaseResponse extraCode(Integer code) {
        this.extraCode = code;
        return this;
    }

    public BaseResponse extraMessage(String message) {
        this.extraMessage = message;
        return this;
    }

    public static BaseResponse success(Object data) {
        return new BaseResponse(data).status(BaseResponseStatusEnum.SUCCESS);
    }

    public static BaseResponse success(String key, Object value) {
        return success(Collections.singletonMap(key, value));
    }

    public static BaseResponse success() {
        return new BaseResponse(Constants.EMPTY_OBJECT).status(BaseResponseStatusEnum.SUCCESS);
    }

    public static BaseResponse failure() {
        return new BaseResponse(Constants.EMPTY_OBJECT).status(BaseResponseStatusEnum.FAILURE);
    }

}
